/**
 * Copyright (C), 2022-2025, www.bosssof.com.cn
 * @FileName UserController.java
 * @Author Administrator
 * @Date 2022-9-28  21:35
 * @Description 该文件为具体的用户管理界面的对应的控制器类
 * History:
 * <author> Administrator
 * <time> 2022-9-28  21:35
 * <version> 1.0.0
 * <desc> 该文件为具体的用户管理界面的对应的控制器类
 */
package com.sd365.permission.centre.api.controller;

import com.sd365.permission.centre.api.UserApi;
import com.sd365.common.core.common.controller.AbstractController;
import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;

import com.sd365.permission.centre.pojo.dto.UserDTO;
import com.sd365.permission.centre.entity.Department;
import com.sd365.permission.centre.entity.Position;
import com.sd365.permission.centre.entity.Role;
import com.sd365.permission.centre.entity.User;
import com.sd365.permission.centre.pojo.vo.ResourceVO;
import com.sd365.permission.centre.service.LoginService;
import com.sd365.permission.centre.service.UserService;
import com.sd365.permission.centre.pojo.query.UserQuery;
import com.sd365.permission.centre.pojo.vo.UserVO;

import com.sd365.permission.centre.dao.mapper.ResourceMapper;
import com.sd365.permission.centre.dao.mapper.RoleMapper;
import com.sd365.permission.centre.entity.*;
import com.sd365.permission.centre.pojo.dto.UserDTO;
import com.sd365.permission.centre.pojo.vo.AdministrativeVO;
import com.sd365.permission.centre.pojo.vo.ResourceVO;
import com.sd365.permission.centre.service.LoginService;
import com.sd365.permission.centre.service.ResourceService;
import com.sd365.permission.centre.service.RoleService;
import com.sd365.permission.centre.service.UserService;
import com.sd365.permission.centre.pojo.query.UserQuery;
import com.sd365.permission.centre.pojo.vo.UserVO;
import com.sd365.permission.centre.service.exception.UserCentreExceptionCode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import java.util.ArrayList;

import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName UserController
 * @Description 实现UserApi接口 以及显示用户登录的功能 具体看接口api说明
 * @Author yangshaoqi
 * @Date 2020/12/14 21:11
 * @Version 1.0
 **/
@RestController


public class UserController implements UserApi  {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private ResourceMapper resourceMapper;

    /**
     * TODO
     * @param id
     * @return
     */
    @Override
    public List<ResourceVO> getUserResourceVO(Long id) {
        User user = userService.queryById(id);
        if (user == null){
            throw new BusinessException(UserCentreExceptionCode.BUSINESS_COMPANY_COMMONQUERY_EXCEPTION_CODE,new Exception("用户不存在"));
        }
        List<Role> roleList = user.getRoleList();
        List<Long> roleIds = new ArrayList<>();
        for (Role role : roleList){
            if (role == null){
                throw new BusinessException(UserCentreExceptionCode.BUSINESS_COMPANY_COMMONQUERY_EXCEPTION_CODE,new Exception("角色不存在"));
            }
            Long roleId = role.getId();
            roleIds.add(roleId);
        }
        List<Resource> resourceList = resourceMapper.getResourceByRoleId(roleIds);
        List<ResourceVO> resourceVOList = new ArrayList<>();
        for (Resource resource : resourceList){
            ResourceVO resourceVO = BeanUtil.copy(resource,ResourceVO.class);
            resourceVOList.add(resourceVO);
        }
        return resourceVOList;
    }

    @Override
    public Boolean add(UserDTO userDTO) {
        return userService.add(userDTO);
    }

    @Override
    public Boolean remove(Long id, Long version) {
        return userService.remove(id,version);
    }

    @Override
    public Boolean modify(UserDTO userDTO) {
        return userService.modify(userDTO);
    }

    @Override
    public Boolean modifyWithNewRole(@Valid UserDTO[] userDTOS) {
        return userService.modifyWithNewRole(userDTOS);
    }

    @Override
    public List<User> commonQuery(UserQuery userQuery) {
        List<User> userList = userService.commonQuery(userQuery);
        return userList;
    }

    @Override
    public UserVO queryUserById(Long id) {
        User user = userService.queryById(id);
        if (user != null) {
            UserVO UserVO = BeanUtil.copy(user, UserVO.class);
            System.out.println(UserVO);
            return UserVO;
        } else {
            return null;
        }
    }


    @Override
    public List<Role> queryAllRole() {
        List<Role> roleList = userService.queryAllRole();
        return roleList;
    }

    @Override
    public List<Department> queryAllDepartment() {
        List<Department> departmentList = userService.queryAllDepartment();
        return departmentList;
    }

    @Override
    public List<Position> queryAllPosition() {
        List<Position> positionList = userService.queryAllPosition();
        return positionList;
    }

    @Override
    public Boolean batchUpdate(@Valid UserDTO[] userDTOS) {
        return userService.batchUpdate(userDTOS);
    }

    @Override
    public Boolean batchDelete(@Valid UserDTO[] userDTOS) {
        return userService.batchDelete(userDTOS);
    }

    @Override
    public Boolean firstStartMd5() {
        return userService.firstStartMd5();
    }

    @Override
    public Boolean updatePassword(UserDTO userDTO) {
        return userService.updatePassword(userDTO);
    }

    @Override
    public User getUser(String code) {
        return userService.getUser(code);
    }

}
